﻿using CnBlogsCore.Interface;
using CnBlogsCore.Model;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace CnBlogsCore.Services
{
    public class ClientCredentialsService : IClientCredentialsService
    {
        public ClientCredentials GetClientCredential()
        {
            var client = new RestClient(Global.CnBlogsUrl.ClientCredentialsUrl);
            RestRequest request = new RestRequest() { Method = Method.POST };

            request.AddParameter("grant_type", "client_credentials");
            request.AddParameter("client_id", Global.CnBlogsUrl.ClientId);
            request.AddParameter("client_secret", Global.CnBlogsUrl.ClientSecret);

            var response = client.Execute(request);

            ClientCredentials clientCredentials = null;
            if (response.StatusCode == HttpStatusCode.OK)
            {
                clientCredentials = JsonConvert.DeserializeObject<ClientCredentials>(response.Content);
            }
            
            return clientCredentials;
        }
    }
}
